# THIS SCRIPT COMPUTES SUMMARY STATISTICS FOR FIGURE 12, AND PROVIDES
# THE SYNTAX FOR CREATING THE FIGURE ITSELF. IT MUST BE RUN IN CONJUNCTION 
# WITH THE DATASET "ANESCumulative_cleaned.csv", AVAILABLE IN THE REPLICATION 
# MATERIALS ON DATAVERSE. 
rm(list = ls())
library(foreign)
library(car)
library(readstata13)
library(ggplot2)
library(stargazer)
library(grid)
library(gridExtra)
library(survey)
library(dplyr)
library(lemon)
library(ggpubr)
library(reshape2)

# READ IN THE DATA. SELECT THE FILE "ANESCumulative_cleaned.csv"
our.data = read.csv(file.choose())
######################################################################################
######################################################################################
######################################################################################
######################################################################################
######################################################################################
# POSITION ON ANTI-DISCRIMINATION LAWS

# DESIGN
our.design = svydesign(ids=~1, weight=~our.data$VCF0009z,
                       data = our.data)

# POSITION, REPUBLICANS
our.means.republicans = svyby(~protectgays, ~year + as.factor(programmatic), 
                              svymean, design = subset(our.design, republican==1), na.rm = TRUE)
our.means.republicans = as.data.frame(our.means.republicans)
# POSITION, DEMOCRATS 
our.means.democrats = svyby(~protectgays, ~year + as.factor(programmatic), 
                            svymean, design = subset(our.design, democrat==1), na.rm = TRUE)
our.means.democrats = as.data.frame(our.means.democrats)
# BIND THE TWO DATASETS
means.data = rbind(our.means.republicans, our.means.democrats)
names(means.data)[2] = c("programmatic")
levels(means.data$programmatic) = c("Unmatched", "Partially matched", "Matched")
means.data$programmatic = factor(means.data$programmatic, levels = c("Matched",
                                                                     "Partially matched",
                                                                     "Unmatched"))
means.data$partisanship = c(rep("Republicans", 60), 
                            rep("Democrats", 60))
# YEAR
means.data$year = as.numeric(as.character(means.data$year))
# SUBSET
means.data = subset(means.data, protectgays!=0)
means.data = means.data[-c(4)]
means.data$item = rep("Anti-discrimination laws \n(average on four-point index)", length(means.data$year))
means.data.protectgays = means.data
names(means.data.protectgays)[3] = "average"
head(means.data.protectgays)
######################################################################################
######################################################################################
######################################################################################
######################################################################################
######################################################################################
# GAYS IN THE MILITARY

# POSITION, REPUBLICANS
our.means.republicans = svyby(~gaysmilitary, ~year + as.factor(programmatic), 
                              svymean, design = subset(our.design, republican==1), na.rm = TRUE)
our.means.republicans = as.data.frame(our.means.republicans)
# POSITION, DEMOCRATS 
our.means.democrats = svyby(~gaysmilitary, ~year + as.factor(programmatic), 
                            svymean, design = subset(our.design, democrat==1), na.rm = TRUE)
our.means.democrats = as.data.frame(our.means.democrats)
# BIND THE TWO DATASETS
means.data = rbind(our.means.republicans, our.means.democrats)
names(means.data)[2] = c("programmatic")
levels(means.data$programmatic) = c("Unmatched", "Partially matched", "Matched")
means.data$programmatic = factor(means.data$programmatic, levels = c("Matched",
                                                                     "Partially matched",
                                                                     "Unmatched"))
means.data$partisanship = c(rep("Republicans", 60), 
                            rep("Democrats", 60))
# YEAR
means.data$year = as.numeric(as.character(means.data$year))
# SUBSET
means.data = subset(means.data, gaysmilitary!=0)
means.data = means.data[-c(4)]
means.data$item = rep("Gays in the military \n(average on four-point index)", length(means.data$year))
means.data.gaysmilitary = means.data
names(means.data.gaysmilitary)[3] = "average"
head(means.data.gaysmilitary)
######################################################################################
######################################################################################
######################################################################################
######################################################################################
######################################################################################
# ADOPTION BY SAME-SEX COUPLES (% THAT SUPPORT)

# PROPORTIONS, REPUBLICANS
our.props = svytable(~ ssadoption + programmatic + year, design = subset(our.design, republican==1))
republicans.props = prop.table(our.props, 2:3)
republicans.props = as.data.frame(republicans.props)
# PROPORTIONS, DEMOCRATS
our.props = svytable(~ ssadoption + programmatic + year, design = subset(our.design, democrat==1))
democrats.props = prop.table(our.props, 2:3)
democrats.props = as.data.frame(democrats.props)
# BIND THE TWO DATASETS
proportions.data = rbind(republicans.props, democrats.props)
proportions.data = subset(proportions.data, ssadoption==1)
proportions.data$partisanship = c(rep("Republicans", 21), 
                                  rep("Democrats", 21))
# YEAR
proportions.data$year = as.numeric(as.character(proportions.data$year))
# PROGRAMMATIC
proportions.data$programmatic = car::recode(proportions.data$programmatic,
                                            "0 = 'Unmatched';
                                            0.5 = 'Partially matched';
                                            1 = 'Matched'; else = NA")
# CLEAN UP...
attach(proportions.data)
means.data.ssadoption = data.frame(year, programmatic, Freq, partisanship)
means.data.ssadoption$item = rep("Adoption by same-sex couples \n(proportion that support)")
names(means.data.ssadoption)[3] = "average"
detach(proportions.data)
######################################################################################
######################################################################################
######################################################################################
######################################################################################
######################################################################################
# MERGE 
new.data = rbind(means.data.ssadoption, means.data.gaysmilitary, means.data.protectgays)
new.data

# FIGURE 12: TOLERANCE TOWARD GAYS AND LESBIANS, 1988-2020, BY PARTISANSHIP 
# AND MATCHING STATUS
our.plot = ggplot(new.data,
                  aes(year, average, group = partisanship, 
                      linetype =  partisanship)) +
  geom_line(aes(linetype = partisanship), linewidth = 1) +
  labs(linetype = "Partisanship") + 
  xlab("Year") +
  ylab("") +
  # ggtitle("Tolerance Toward Gays and Lesbians, 1988-2020, \nBy Partisanship and Matching Status", 
  #        subtitle = "Data: ANES cumulative file, partisan identifiers only. \nHigher scores indicate more progressive attitudes.") +  
  theme_minimal() +  
  facet_grid(programmatic ~ item, scales = "free_x") +
  # theme(legend.title = element_text(face = "bold")) +
  theme(legend.position = "none") +
  # theme(legend.title = element_text(size = 12)) +
  # theme(legend.text = element_text(size = 12)) +
  theme(axis.title.x = element_text(size = 9)) + 
  theme(axis.title.y = element_text(size = 9)) +
  # theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 14)) + 
  # theme(plot.subtitle = element_text(hjust = 0.5, size = 12)) +
  theme(strip.text = element_text(face = "bold", size = 9)) + ylim(0, 1) +
  theme(panel.spacing = unit(1, "lines"))
our.plot

# SAVE
# ggsave(our.plot, file = "Schmidtetal-Figure12.pdf", width = 7, height = 9)
